package com.oyjd.ybx.business.talk.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oyjd.ybx.bean.po.ProLine;
import com.oyjd.ybx.bean.po.Talk;
import com.oyjd.ybx.bean.po.User;
import com.oyjd.ybx.bean.vo.ProLineVO;
import com.oyjd.ybx.bean.vo.UserVO;
import com.oyjd.ybx.business.sys.dao.SysDao;
import com.oyjd.ybx.business.talk.dao.TalkDao;
import com.oyjd.ybx.business.talk.service.TalkService;
import com.oyjd.ybx.business.talk.vo.CustVO;
import com.oyjd.ybx.business.talk.vo.TalkExVO;

import edu.emory.mathcs.backport.java.util.Collections;

/**
 * 
 * 在线交流业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class TalkServiceImpl implements TalkService {
	private TalkDao dao;

	@Autowired
	public void setDao(TalkDao dao) {
		this.dao = dao;
	}

	private SysDao sysDao;

	@Autowired
	public void setDao(SysDao sysDao) {
		this.sysDao = sysDao;
	}

	/**
	 * 获取在线交流列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public List<TalkExVO> getList(String talkAccount, String getAccount) {
		UserVO t1 = sysDao.getUser(talkAccount);
		UserVO t2 = sysDao.getUser(getAccount);
		List<TalkExVO> talkVOs = dao.getList(talkAccount, getAccount);
		for (TalkExVO vo : talkVOs) {
			if (vo.getTalkAccount().equals(talkAccount)) {
				vo.setTalkAccountName(null != t1 ? t1.getName() : "过客");
				vo.setRequest(true);
			} else if (vo.getTalkAccount().equals(getAccount)) {
				vo.setTalkAccountName(null != t2 ? t2.getName() : "过客");
				vo.setRequest(false);
			}

			if (vo.getGetAccount().equals(talkAccount)) {
				vo.setGetAccountName(null != t1 ? t1.getName() : "过客");
			} else if (vo.getGetAccount().equals(getAccount)) {
				vo.setGetAccountName(null != t2 ? t2.getName() : "过客");
			}
		}
		Collections.reverse(talkVOs);
		return talkVOs;
	}

	/**
	 * 增加在线交流
	 * 
	 * @param talkAccount
	 *            对话帐号
	 * @param getAccount
	 *            受话帐号
	 * @param content
	 *            对话内容
	 */
	@Transactional(readOnly = false)
	public void add(String talkAccount, String getAccount, String content) {
		Date date = new Date();
		Talk talk = new Talk();
		talk.setTalkAccount(talkAccount);
		talk.setGetAccount(getAccount);
		talk.setContent(content);
		talk.setCreateTime(date);
		dao.create(talk);
	}

	/**
	 * 获取在线专家
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ProLineVO> getPro() {
		return (List<ProLineVO>) dao.queryByHql("select new " + ProLineVO.class.getName() + ProLineVO.STRUCT + " from " + ProLine.class.getName() + " proline");
	}

	/**
	 * 专家上线
	 * 
	 * @param account
	 * @param name
	 */
	@Transactional(readOnly = false)
	public void addPro(String account, String name, int type) {
		dao.executeHql("delete from " + ProLine.class.getName() + " where account=?", new Object[] { account });
		ProLine pLine = new ProLine();
		pLine.setAccount(account);
		pLine.setName(name);
		pLine.setType(type);
		dao.create(pLine);
	}

	/**
	 * 专家下线
	 * 
	 * @param account
	 * @param name
	 */
	@Transactional(readOnly = false)
	public void removePro(String account) {
		dao.executeHql("delete from " + ProLine.class.getName() + " where account=?", new Object[] { account });
	}

	/**
	 * 获取与专家对话的人物列表
	 * 
	 * @param account
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<CustVO> getTalkerForPro(String account) {
		List<CustVO> tmpCustVOs = dao.getTalkerForPro(account);
		List<User> list = (List<User>) dao.queryAll(User.class);
		Map<String, String> map = new HashMap<String, String>();
		for (User u : list) {
			map.put(u.getAccount(), u.getName());
		}
		for (CustVO v : tmpCustVOs) {
			if (map.containsKey(v.getCustId())) {
				v.setCustName(map.get(v.getCustId()));
			} else {
				v.setCustName("过客");
			}
		}
		return tmpCustVOs;
	}
}
